<?php

/**
 * Description of UserController
 *
 * @author gun
 */

require_once 'BaseController.php';
class Admin_UserController extends Admin_BaseController {
    
    public function init() {
        parent::init();
        
    }
    
    public function indexAction() {
        $request = $this->getRequest();
        
        if ($this->httpRequest()->isXmlHttpRequest()) {
            $q = $request->getParam('q');
            $page = $request->getParam('page');
            
            $where = array('name LIKE ?' => "%$q%");
            $stat = $this->_helper->ListPage('Admin_Model_UserService', $page, $where);
            
            $options = array(
                'limit'=>
                    array(
                        $stat['perpage'] => $stat['offset'])                
            );
            
            $service = AppRegistry::loadService('Admin_Model_UserService');
            $data = $service->getAll($where, $options);
            
            if ($data) {
                $this->_helper->json(array(
                    'data' => $data, 
                    'pview' => $stat['pview'],
                    'offset' => $stat['offset'],
                    'fieldname' => 'level'
                ));
                
            } else {
                $this->_helper->json(array('error'=>'No User Found.'));
            }
        }        
    }
    
    public function addAction() {
        $service = AppRegistry::loadService('Admin_Model_GroupService');
        $getGroup = $service->getGroup();                

        $group = $this->_helper->CatList($getGroup);
        $this->view->level = $group;
    }
    
    public function editAction() {
        $id = $this->getParam('id');
        $where = array('userID = ?' => $id);
        
        $service = AppRegistry::loadService('Admin_Model_UserService');
        $data = $service->getRow($where);

        $service2 = AppRegistry::loadService('Admin_Model_GroupService');
        $getGroup = $service2->getGroup();
        
        $group = $this->_helper->CatList($getGroup, $data['level']);
       
        $this->view->level = $group;
        $this->view->data = $data;
    }
    
    public function saveAction() {
        $id = $this->getParam('id');
        $name = $this->getParam('name');
        $level = $this->getParam('level');
        
        $data = array(
            'name' => $name,
            'level' => $level
        );

        foreach($this->baseUserLogin($id) as $key => $value) $data[ $key ] = $value;

            try {
                $service = AppRegistry::loadService('Admin_Model_UserService');        
                $save = (!isset($id) ? $service->doSave($data) : $service->doUpdate($data, array('userID = ?' => $id)));
                
                $this->_helper->json(array('status'=>($save ? 'Save Success.' : 'Nothing changed.')));
                
            } catch (Exception $exc) {
                $this->_helper->json(array('status'=>$exc->getMessage()));            
            }
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function deleteAction() {
        try {
            $id = $this->getParam('id');
            $service = AppRegistry::loadService('Admin_Model_UserService');
        
            $delete = $service->doDelete($id);
            $this->_helper->json(array('status'=>($delete ? 'Delete success.' : 'Nothing deleted.')));
            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getTraceAsString()));
        }
    }            
    
    public function changePasswordAction() {
        $obj = new Zend_Session_Namespace(Admin_SecureController::$_NS);
        $user = $obj->user->name;
        
        $service = AppRegistry::loadService('Admin_Model_UserService');
        $data = $service->getRow(array('name = ?' => $user));
        
        $this->view->data = $data;
    }
    
    public function processChPasswordAction() {
        $id = $this->getParam('id');
        $oldpassword = $this->getParam('oldpassword');
        $newpassword = $this->getParam('newpassword');
        
        $where = array(
            'userID = ?' => $id,
            'password = ?' => md5($oldpassword)                
            );
        
        $data = array(            
            'password' => md5($newpassword)
        );

        $service = AppRegistry::loadService('Admin_Model_UserService');               
        $check = $service->getRow($where);
        
        if ($check) {
            $update = $service->doUpdate($data, $where);
            
            $this->_helper->json(
                ($update) ? 
                    array('status'=>TRUE, 'message'=>'Password Updated.') :
                    array('status'=>FALSE, 'message'=>'Failed Update Password.')
                );
            
        } else {
            $this->_helper->json(array('status'=>FALSE, 'message'=>'Your current password does\'nt match with database.'));
        }
        
        $this->_helper->viewRenderer->setNoRender();
    }
}
